﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigDrugMethodsCtrlViewModel : BaseViewModel<ConfigDrugMethodsCtrlViewModel>
    {
        #region Variables
        private string _seachValue = "";

        private ObservableCollection<ConfigDrugMethodsModel> _displayConfigDrugMethodsModels = new ObservableCollection<ConfigDrugMethodsModel>();

        #endregion

        #region Constructor

        public ConfigDrugMethodsCtrlViewModel()
        { }



        #endregion

        #region Properties



        public ObservableCollection<ConfigDrugMethodsModel> DisplayConfigDrugMethodsModels
        {
            get
            {
                _displayConfigDrugMethodsModels.Clear();
                List<ConfigDrugMethodsModel> lst = ConfigDrugMethodsDataGridViewModel.Instance.ConfigDrugMethodsModel.Where(O =>
                O.Name.Contains(_seachValue) || O.Code.Contains(_seachValue)).ToList();
                foreach (var item in lst)
                {
                    _displayConfigDrugMethodsModels.Add(item);
                    item.PropertyChanged += Model_PropertyChanged;
                }
                return _displayConfigDrugMethodsModels;
            }

            set
            {
                ConfigDrugMethodsDataGridViewModel.Instance.ConfigDrugMethodsModel = value;
                RaisePropertyChanged("DisplayConfigDrugMethodsModels");
            }
        }

        public string SeachValue
        {
            get
            {
                return _seachValue;
            }

            set
            {
                _seachValue = value;
                RaisePropertyChanged("DisplayConfigDrugMethodsModels");
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void AddConfigDrugMethodsModels(ConfigDrugMethodsModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigDrugMethodsService.Instance.Update(obj);
                ConfigDrugMethodsDataGridViewModel.Instance.ConfigDrugMethodsModel.Add(obj);
            }
            else ConfigDrugMethodsService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigDrugMethodsModel(ConfigDrugMethodsModel configDrugMethodsModel)
        {
            if (configDrugMethodsModel != null)
            {
                ConfigDrugMethodsDataGridViewModel.Instance.ConfigDrugMethodsModel.Remove(configDrugMethodsModel);
                RaisePropertyChanged("DisplayConfigDrugMethodsModels");
                ConfigDrugMethodsService.Instance.DeleteById(configDrugMethodsModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDrugMethodsModel model = new ConfigDrugMethodsModel();
            DisplayConfigDrugMethodsModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }

        #endregion

        #region EventHandlers

        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDrugMethodsModels(sender as ConfigDrugMethodsModel);
        }
        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
